package service

import (
	"context"
	"github.com/kuangshp/go-utils/k"
	"github.com/shopspring/decimal"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"time"
)

type _purchaseStoreDetailService struct{}

func (s _purchaseStoreDetailService) CreateBatchApi(ctx context.Context, tx *dao.QueryTx, data []*model.PurchaseStoreDetailEntity) error {
	if err := tx.PurchaseStoreDetailEntity.WithContext(ctx).CreateInBatches(data, len(data)); err != nil {
		return err
	}
	return nil
}

func (s _purchaseStoreDetailService) GetPurchaseStoreByParentIdApi(ctx context.Context, id int64) ([]*model.PurchaseStoreDetailEntity, error) {
	return dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.PurchaseStoreID.Eq(id)).Find()
}
func (s _purchaseStoreDetailService) GetPurchaseStoreByPurchaseStoreIdSpuIdApi(ctx context.Context, purchaseStoreId, spuId int64) (*model.PurchaseStoreDetailEntity, error) {
	return dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(
		dao.PurchaseStoreDetailEntity.PurchaseStoreID.Eq(purchaseStoreId),
		dao.PurchaseStoreDetailEntity.SpuID.Eq(spuId),
	).First()
}

func (s _purchaseStoreDetailService) ModifyPurchaseStoreDetailByIdApi(ctx context.Context, tx *dao.QueryTx, id int64, storeCount, storeAmount decimal.Decimal, shelfId int64, batchNumber string, storeStatus int64) error {
	if _, err := tx.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseStoreDetailEntity.StoreCount.Value(storeCount),
		dao.PurchaseStoreDetailEntity.StoreAmount.Value(storeAmount),
		dao.PurchaseStoreDetailEntity.StoreStatus.Value(storeStatus),
		dao.PurchaseStoreDetailEntity.ShelfID.Value(shelfId),
		dao.PurchaseStoreDetailEntity.BatchNumber.Value(batchNumber),
		dao.PurchaseStoreDetailEntity.StorageTime.Value(time.Now()),
	); err != nil {
		return err
	}
	return nil
}

func (s _purchaseStoreDetailService) AddPurchaseStoreDetailByIdApi(ctx context.Context, tx *dao.QueryTx, id int64, storeCount, storeAmount decimal.Decimal, storeStatus int64) error {
	if _, err := tx.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseStoreDetailEntity.StoreCount.Value(storeCount),
		dao.PurchaseStoreDetailEntity.StoreAmount.Value(storeAmount),
		dao.PurchaseStoreDetailEntity.StoreStatus.Value(storeStatus),
	); err != nil {
		return err
	}
	return nil
}

func (s _purchaseStoreDetailService) GetTotalAmountByPurchaseStoreIdListApi(ctx context.Context, list []int64) map[int64]decimal.Decimal {

	sortingDetailEntities, err := dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.PurchaseStoreID.In(list...)).Find()
	var resultMap = make(map[int64]decimal.Decimal)
	if err == nil && len(sortingDetailEntities) > 0 {
		sortingDetailGroup := k.GroupBy(sortingDetailEntities, func(item *model.PurchaseStoreDetailEntity) int64 {
			return item.PurchaseStoreID
		})
		for key, item := range sortingDetailGroup {
			var currentAmount = decimal.Zero
			for _, item1 := range item {
				currentAmount = currentAmount.Add(item1.StoreAmount)
			}
			resultMap[key] = currentAmount
		}
	}
	return resultMap

}

func (s _purchaseStoreDetailService) GetPurchaseStoreByPurchaseOrdersDetailIdApi(ctx context.Context, id int64) (*model.PurchaseStoreDetailEntity, error) {
	return dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.PurchaseOrdersDetailID.Eq(id)).First()
}

func (s _purchaseStoreDetailService) GetPurchaseStoreByPurchaseOrdersDetailIdListApi(ctx context.Context, orderDetailIdList []int64) ([]*model.PurchaseStoreDetailEntity, error) {
	return dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.PurchaseOrdersDetailID.In(orderDetailIdList...)).Find()
}

func (s _purchaseStoreDetailService) GetPurchaseStoreByPurchaseStoreDetailIdApi(ctx context.Context, id int64) (*model.PurchaseStoreDetailEntity, error) {
	return dao.PurchaseStoreDetailEntity.WithContext(ctx).Where(dao.PurchaseStoreDetailEntity.ID.Eq(id)).First()
}

func (s _purchaseStoreDetailService) CreateApi(ctx context.Context, tx *dao.QueryTx, m *model.PurchaseStoreDetailEntity) error {
	if err := tx.PurchaseStoreDetailEntity.WithContext(ctx).Create(m); err != nil {
		return err
	}
	return nil
}

var PurchaseStoreDetailService = new(_purchaseStoreDetailService)
